<?php
namespace Resmodel;

use Core\Config;
use Core\Constant;
use Core\Exception\BusinessException;
use Logger;
use Model\LanguageAwareInterface;
use Resmodel\Factory\ResTableGatewayFactory;
use Zend\Db\Adapter\Adapter;
use Zend\Db\Sql\Predicate\Like;
use Zend\Db\Sql\Predicate\Operator;
use Zend\Db\Sql\Select;
use Zend\InputFilter\InputFilter;


class UnitTable extends ResAbstractTable implements LanguageAwareInterface
{

    /**
     * @var string
     */
    private $_lang;

    /**
     * @param $id
     * @return Unit
     */
    public function getUnit($id){
       return $this->getById($id);
    }

    /**
     * @return InputFilter
     */
    public function getInputFilter()
    {
        $filter = new InputFilter();
        return $filter;
    }


    public function getLang()
    {
        return $this->_lang;
    }

    public function setLang($lang)
    {
        $this->_lang = $lang;
    }

    public function getAllAsArray(){
        $select = $this->getDbSqlSelect();
        $select->join(array('ul'=>'unit_lang'),'ul.unit_id = unit.id', array('name'=>'name'))
        ->where(array('ul.lang'=>$this->getLang()));
//        Logger::debug($select->getSqlString($this->getTableGateway()->getAdapter()->platform));
        $resultSet = $this->getTableGateway()->selectWith($select);
        $returnArr = array();
        foreach ($resultSet as $item) {
            $returnArr[] =  $item->toArray();
        }
        return $returnArr;
    }

    /**
     * @param $name
     * @return Unit
     */
    public function getUnitByName($name){
        $select = $this->getDbSqlSelect();
        $select->join(array('ul'=>'unit_lang'),'ul.unit_id = unit.id', array('name'=>'name'))
            ->where(array('ul.lang'=>$this->getLang(), 'name'=>$name));
        $resultSet = $this->getTableGateway()->selectWith($select);
        foreach ($resultSet as $anUnit) {
            return $anUnit;
        }

    }


    public function addUnit($arr){
        $anUnit = $this->getUnitByName($arr['name']);
        if($anUnit) throw new BusinessException('Đã tồn tại đơn vị có tên: '.$arr['name']);

        $unitId = $this->insert(array('dump'=>''));
        $unitLangTable = ResTableGatewayFactory::getUnitLangTable();
        $unitLangTable->insert(array('unit_id'=>$unitId, 'name'=>$arr['name']));

        $allLangCodes = Config::getLangCodes();
        foreach ($allLangCodes as $aLang) {
            if($aLang != $this->getLang())
                $unitLangTable->insert(array('lang'=>$aLang, 'unit_id'=>$unitId,
                    'name'=>Constant::$DEFAULT_LANG_NEED_UPDATE));
        }

        return $unitId;
    }

    public function updateUnit($arr){
        $anUnit = $this->getUnitByName($arr['name']);
        if($anUnit && $anUnit->id != $arr['id'])
            throw new BusinessException('Đã tồn tại đơn vị có tên: '.$arr['name']);

        $unitLangTable = ResTableGatewayFactory::getUnitLangTable();
        $unitLang = $unitLangTable->getByUnitAndLang($arr['id'], $this->getLang());
        if(!$unitLang)
            throw new BusinessException('Bản ghi không tồn tại!');

        $unitLangTable->update(array( 'name'=>$arr['name']), $unitLang->id);
    }

    public function deleteUnit($id){
        $this->delete($id);
        $unitLangTable = ResTableGatewayFactory::getUnitLangTable();
        $unitLangTable->tableGateway->delete(array('unit_id'=>$id));
    }

}